/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Modele.Contact;
import Modele.FileManipulation;
import Modele.GetGroup;
import Modele.Group;
import Modele.TableModelContact;
import Modele.TableRenderer;
import Vue.ContactPanel;
import Vue.ViewContactPanel;
import java.awt.Color;
import java.awt.ScrollPane;
import Vue.Window;
import java.awt.Font;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Bakalian
 */
public class GroupListener implements MouseListener {

    private List<Contact> myList;
    private JPanel contactPanelToModify;
    private ContactPanel cp;
    private ViewContactPanel displayPanel;
    private int selectedGroup=0;
    private  String groupToShowString=null;
    private Window myWindow;
    
    
   // public GroupListener (JPanel c){
     public GroupListener( ContactPanel c,ViewContactPanel vpc,Window w) {
     //contactPanelToModify=c;
        displayPanel=vpc;
        cp=c;
        myWindow=w;
        
        
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        /**
         * step of the action:
         * we get the Jlabel we clicked on to know which group he want to see
         * we record the number & name of the group
         * we send it to the SorteCont of Filemanipulation which returns the list of contact belonging to this group
         * we get the model of the jtable
         * we change it the data, to update the JTABLe if not done the jtable doesn't update
         * 
         * need to change the model content to update jtable
         * don't forget to add accurate viewlistener on the new Jtable because of new model
         * the the smae step than contact Panel
         */
        FileManipulation fm=new FileManipulation();// on alloue un nouveau file ma,nipulation pour obtenir la liste des contact
        List<Contact> newlist;
        JLabel tmp;
        tmp=(JLabel) e.getComponent();// on receptionne le jlabe qui a ete cliqué
        
        GetGroup gg=new GetGroup();
        List<Group> listofGroup=gg.getGroups();//
        
        for(int i=0;i<listofGroup.size();i++){
            myWindow.getMyGroupPanel().getBackground().getComponent(i).setFont(new Font("Arial",1,12));
            myWindow.getMyGroupPanel().getBackground().getComponent(i).setForeground(Color.gray);
        }
      
        
        for(int j=0;j<listofGroup.size();j++){
            if(tmp.getName().equals(listofGroup.get(j).getNamegroup())){
                tmp.setForeground(Color.black);
                tmp.setFont(new Font("Arial",1,17));
                selectedGroup=listofGroup.get(j).getNumeroGroup();
                groupToShowString=listofGroup.get(j).getNamegroup();
                
            }
            else {
                
            }
        }
        
      
      myWindow.setMyListofContact(fm.getSortedContact(selectedGroup));
      
     myWindow.getMyContactPanel().getMyTable().setModel(new TableModelContact(myWindow.getMyListofContact()));
     TableRowSorter<TableModelContact> sorter = new TableRowSorter<>((TableModelContact) myWindow.getMyContactPanel().getMyTable().getModel());
     myWindow.getMyContactPanel().getMyTable().setRowSorter(sorter);
     myWindow.getMyContactPanel().getMyTable().setRowSorter(sorter);
     sorter.setSortsOnUpdates(true);
     TableRenderer buttonRenderer = new TableRenderer();
     myWindow.getMyContactPanel().getMyTable().getColumnModel().getColumn(0).setCellRenderer(buttonRenderer);
     
       //newlist=fm.getSortedContact(selectedGroup);
    //  cp.getMyTable().setModel(new TableModelContact(newlist));
    //TableRowSorter<TableModelContact> sorter = new TableRowSorter<>((TableModelContact) cp.getMyTable().getModel());
   // System.out.println("number of mouselistener on jtable " + cp.getMyTable().getMouseListeners().length);
    /*for(int i=0;i<cp.getMyTable().getMouseListeners().length;i++){
    cp.getMyTable().removeMouseListener(cp.getMyTable().getMouseListeners()[i]);
    }
    System.out.println("number of mouselistener on jtable " + cp.getMyTable().getMouseListeners().length);
    System.out.println("size of list sent " + newlist.size());
    cp.getMyTable().setRowSorter(sorter);
    cp.getMyTable().addMouseListener(new AccurateViewListener(cp.getMyTable(),newlist,displayPanel));//-----------------to put
   sorter.setSortsOnUpdates(true);*/
   
  // TableRenderer buttonRenderer = new TableRenderer();
    //myTable.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(myTable, sorter, true, true, 0, 0).setPreferredSize(new Dimension(20,20));
    // cp.getMyTable().getColumnModel().getColumn(0).setCellRenderer(buttonRenderer);
      
      
      
        
        /**/
        
        
        
       
        
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mousePressed(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseEntered(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public int getSelectedGroup() {
        return selectedGroup;
    }

    public void setSelectedGroup(int selectedGroup) {
        this.selectedGroup = selectedGroup;
    }
    
}
